package com.smart.controller;

import com.smart.common.WebResponse;
import com.smart.common.PageInfo;
import com.smart.common.annotation.WebAccess;
import com.smart.model.request.JiansheUnitAddRequest;
import com.smart.model.request.JiansheUnitDeleteProjectRequest;
import com.smart.model.request.JiansheUnitRequest;
import com.smart.model.request.JiansheUnitUpdateRequest;
import com.smart.model.vo.ProjectVO;
import com.smart.service.JiansheUnitService;
import com.smart.model.vo.JiansheUnitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/jianSheUnit")
@Api(tags = {"云监管平台#建设单位模块"})
public class JiansheUnitController {

    @Autowired
    private JiansheUnitService jiansheUnitService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<JiansheUnitVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(jiansheUnitService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<JiansheUnitVO>> pageQuery(@RequestBody JiansheUnitRequest request) {
        return WebResponse.build(jiansheUnitService.pageQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("列表查询")
    @PostMapping("/listQuery")
    public WebResponse<List<JiansheUnitVO>> listQuery(@RequestBody JiansheUnitRequest request) {
        return WebResponse.build(jiansheUnitService.listQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("新增")
    @PostMapping("/add")
    public WebResponse<Boolean> add(@Valid @RequestBody JiansheUnitAddRequest request) {
        return WebResponse.build(jiansheUnitService.add(request));
    }

    @WebAccess(login = true)
    @ApiOperation("修改")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody JiansheUnitUpdateRequest request) {
        return WebResponse.build(jiansheUnitService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("重置密码")
    @PostMapping("/resetPassword/{id}")
    public WebResponse<Boolean> resetPassword(@Valid @PathVariable("id") long id) {
        return WebResponse.build(jiansheUnitService.resetPassword(id));
    }

    @WebAccess(login = true)
    @ApiOperation("删除项目")
    @PostMapping("/deleteProject")
    public WebResponse<Boolean> deleteProject(@Valid @RequestBody JiansheUnitDeleteProjectRequest request) {
        return WebResponse.build(jiansheUnitService.deleteProject(request));
    }

    @WebAccess(login = true)
    @ApiOperation("根据建设单位名称精确搜索项目注册信息中建设单位一致的项目")
    @PostMapping("/queryByName/{unitName}")
    public WebResponse<List<ProjectVO>> queryByName(@Valid @PathVariable("unitName") String unitName) {
        return WebResponse.build(jiansheUnitService.queryByName(unitName));
    }
}
